<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\controller\AdminLogin;
use app\common\model\AdminMenuModel;
use app\common\model\UploadListModel;
use app\common\service\AdminMenuService;
use app\common\service\AdminUserService;
use app\common\service\RegionService;


class Index extends AdminLogin
{
    protected $middleware = ['app\middleware\AdminLogin'];

    /*
     * @title 框架首页
     * */
    public function index()
    {
        //菜单
        $id = $this->request->param('id', '');
        $menu_list = (AdminMenuService::ins())->getRoleMenu($this->request->user_info['role_id']); //获取管理员的全部菜单
        if (empty($menu_list)) {
            AdminUserService::ins()->clearLogin();
            return $this->error('您没有任何权限,请联系管理员');
        }

        $all_menu = [];
        $user_menu = [];
        foreach ($menu_list as $key => $value) {
            $all_menu[] = [
                'id' => $value['id'],
                'name' => $value['name'],
                'icon' => $value['icon']
            ];
            if ($value['id'] == $id) {
                $user_menu = $value['children'];
            }
        }

        if (empty($user_menu)) {
            $user_menu = $menu_list[0]['children'];
        }

        if (empty($id)) {
            $id = $all_menu[0]['id'];
        }

        //主题，皮肤
        $skin_list = [
            1 => ['id' => 1, 'name' => '主题1', 'theme' => 'theme1.css'],
            2 => ['id' => 2, 'name' => '主题2', 'theme' => 'theme2.css'],
            3 => ['id' => 3, 'name' => '主题3', 'theme' => 'theme3.css'],
            4 => ['id' => 4, 'name' => '主题4', 'theme' => 'theme4.css'],
            5 => ['id' => 5, 'name' => '主题5', 'theme' => 'theme5.css'],
            6 => ['id' => 6, 'name' => '默认', 'theme' => null],
        ];
        $skin_id = $this->request->param('skin', '-1');
        if (isset($skin_list[$skin_id])) {
            cookie('skin', $skin_list[$skin_id]['theme']);
        }
        $skin = cookie('skin');
        if (!$skin) {
            $skin = '';
        }

        $this->assign('id', $id)
            ->assign('page_size', $this->page_size)
            ->assign('theme', $skin)
            ->assign('skin_list', $skin_list)
            ->assign('all_menu', $all_menu)
            ->assign('user_menu', $user_menu)
            ->assign('user_info', $this->request->user_info)
            ->assign('env', getenv('ENV'));

        return view();
    }

    /*
     * @title 我的桌面
     * */
    public function welcome()
    {
        //获取mysql版本
        $result = \think\facade\Db::query('select version() as ver');
        $mysql_version = '暂时无法获取';
        if ($result) {
            $mysql_version = $result[0]['ver'];
        }
        $this->assign('mysql_version', $mysql_version)
            ->assign('user_info', $this->request->user_info);
        return view();
    }

    /*
     * @获取统计信息
     * */
    public function summaryData()
    {
        $s = $this->request->param('start_date', date('Y-m-d', strtotime('-7 day')));
        $e = $this->request->param('end_date', date('Y-m-d'));

        $pattern = '/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/';
        if (!preg_match($pattern, $s)) {
            return $this->error('起始日期错误');
        }
        if (!preg_match($pattern, $e)) {
            return $this->error('结束日期错误');
        }
        //初始化折线图信息
        $line_chart = [];
        $line_chart['title'] = '折线图堆叠';
        $line_chart['legend'] = []; //注释
        $line_chart['date'] = []; //日期
        $line_chart['series'] = []; //数据

        $info = AdminUserService::ins()->lineChartData($s, $e);
        if (!empty($info)) {
            $line_chart['legend'] = array_merge($line_chart['legend'], $info['legend']);
            $line_chart['date'] = array_merge($line_chart['date'], $info['date']);
            $line_chart['series'] = array_merge($line_chart['series'], $info['series']);
        }
        return $this->success('成功', $line_chart);
    }


    /*
     * @title 获取菜单帮助信息
     * */
    public function menuInfo()
    {
        $id = $this->request->param('id', '');
        if (!$id) {
            $this->error('参数错误');
        }
        $info = AdminMenuModel::where('id', $id)->find();
        if (!$info) {
            return $this->error('参数错误');
        }
        $data['info'] = $info->getAttr('desc');
        return $this->success('获取成功', $data);
    }

    /*
     * @title 文件上传
     * */
    public function upload()
    {
        if ($this->request->isPost()) {
            $field = $this->request->param('field', 'file'); //FILES上传的文件的字段名称
            $save = $this->request->param('save', ''); //保存的文件名称
            $big = $this->request->param('big', 0); //大文件上传,追加方式
            $edit = $this->request->param('edit', 0); //编辑器,文件上传
            $n_edit = $this->request->param('n_edit', 0); //新编辑器,文件上传
            $introduce = $this->request->param('introduce', ''); //文件简介

            $Upload = upload_obj();
            if ($big == 1) {
                $result = $Upload->uploadBig($field, $save);
            } else {
                $result = $Upload->upload($field, $save);
            }
            //编辑器上传时特殊返回
            if ($edit == 1) {
                if (!$result) {
                    return json(['code' => 1, 'msg' => $Upload->error, []]);
                }
                (new UploadListModel())->addLog($this->request->user_info['uid'], $result['url'], 2, $introduce);
                return json([
                    'code' => 0,
                    'msg' => 'success',
                    'data' => ['src' => $result['url'], 'title' => '']
                ]);
            } elseif ($edit == 2) {
                if (!$result) {
                    return json(['errno' => 1, 'message' => $Upload->error, []]);
                }
                (new UploadListModel())->addLog($this->request->user_info['uid'], $result['url'], 2, $introduce);
                return json([
                    'errno' => 0,
                    'message' => 'success',
                    'data' => ['url' => $result['url'],'href' => $result['url'], 'alt' => '']
                ]);
            } else {
                if (!$result) {
                    return $this->error($Upload->error);
                }
                if ($big != 1) {
                    (new UploadListModel())->addLog($this->request->user_info['uid'], $result['url'], 1, $introduce);
                }
                return $this->success('上传成功', $result);
            }
        } else {
            return view();
        }
    }

    /*
     * @title 设置每页显示条数
     * */
    public function setPageSize()
    {
        $page_size = $this->request->param('page_size', 20);
        if (empty($page_size) || $page_size < 10) {
            $this->error('参数错误');
        }
        cookie('page_size', $page_size, 86400 * 30);
        return $this->success('设置成功,请重新刷新页面');
    }

    /*
     * @title 获取区域选择的json数据
     * */
    public function regionJson()
    {
        $data = (new RegionService())->mapJson();
        return json($data);
    }

    /*
     * @title 图标字体
     * */
    public function unicode()
    {
        return view();
    }

    /*
     * @title 富文本编辑器
     * */
    public function layedit()
    {
        return view();
    }
    /*
     * @title 富文本编辑器
     * */
    public function layeditNew()
    {
        return view();
    }
}
